
#pragma once

#include <vsg/all.h>
#include <vsgQt/Window.h>
#include <QMainWindow>
#include <QVBoxLayout>
#include <QWidget>
#include <QWindow>

#include "./components.hpp"
#include "./materials.hpp"
#include "./meshs.hpp"
#include "./objects.hpp"
#include "./task.hpp"
#include "./tools.hpp"

#include "../axis/axis_plugin.hpp"
#include "../canvas/CanvasWidget.h"
#include "../ecs/ecs.hpp"
#include "../input/input.hpp"
#include "../utils/render_line.h"
#include "../utils/render_pin.h"
#include "../utils/render_ruler.h"

namespace qbe::input {
class HandleBundle;
class MouseHandle;
class KeyBoardHandle;
class WindowHandle;
}  // namespace qbe::input

namespace qbe::render {
using namespace input;

using res_window_traits_t = ecs::Resource<vsg::ref_ptr<vsg::WindowTraits>>;
using res_options_t = ecs::Resource<vsg::ref_ptr<vsg::Options>>;

enum State {
  None,
  AddWidget,
};

struct SceneTag {};

class Scene : public vsg::Inherit<vsg::Group, Scene> {};

struct ViewerBundle {
  vsg::ref_ptr<vsg::Group> scene;
  vsg::ref_ptr<vsg::Camera> camera;
  vsg::ref_ptr<vsg::WindowTraits> windowTraits;
  vsg::ref_ptr<vsg::Options> options;
  vsg::ref_ptr<vsgQt::Viewer> viewer;

  vsgQt::Window* window;
  QWidget* widget;

  std::shared_ptr<vsgQt::Window> window_ptr;
  std::shared_ptr<QWidget> widget_ptr;

  QMainWindow* mainWindow;
};

struct Model {
  vsg::ref_ptr<vsg::WindowTraits> windowTraits;
  vsg::ref_ptr<vsg::Options> options;
  vsg::ref_ptr<vsgQt::Viewer> viewer;
  vsgQt::Window* window;
  vsgQt::Window* map_window;
  // VsgWindow* window;
  // VsgWindow* map_window;

  vsg::ref_ptr<vsg::Camera> camera;
  vsg::ref_ptr<vsg::Camera> map_camera;

  vsg::ref_ptr<vsg::Group> scene;
  vsg::ref_ptr<vsg::MatrixTransform> transform;

  vsg::ref_ptr<vsg::Group> cell_group;
  vsg::ref_ptr<vsg::Group> axis_group;    // 坐标绘制
  vsg::ref_ptr<vsg::Group> edit_group;    // 编辑绘制
  vsg::ref_ptr<vsg::Group> select_group;  // 选择绘制

  bool is_compile;

  std::shared_ptr<utils::TempRender::RenderRuler> m_render_rulers;
  std::shared_ptr<utils::TempRender::RenderPin> m_render_pins;
  std::shared_ptr<utils::TempRender::RenderLine> m_render_flylines;
};

// void create_viewer(entt::registry& reg, ecs::Commands commands, ecs::Resource<vsg::ref_ptr<HandleBundle>> handles);
void create_viewer(ecs::World& world, entt::registry& reg, ecs::Commands commands, ecs::Resource<vsg::ref_ptr<input::HandleBundle>> handles,
                   ecs::Query query);
void handle_event(entt::registry& reg);
void update_render(entt::registry& reg);
// void update_camera(entt::registry& reg, ecs::Resource<vsg::ref_ptr<input::HandleBundle>> handles,
//                    ecs::Resource<vsg::ref_ptr<vsg::WindowTraits>> res_traits);
void add_widget(entt::registry& reg, ecs::Resource<State> state, ecs::Resource<QVBoxLayout*> ui_layout);

class Render : public ecs::Plugin {
 public:
  void build(ecs::App& app) override;
};

}  // namespace qbe::render
